﻿#include "qt_hread_process.h"



void callback(Mat mat_image, void* p)
{

    QtThreadProcess* pCammerWidget = static_cast<QtThreadProcess*>(p);

    pCammerWidget->m_image_index++;

    if (pCammerWidget->m_proess_state == true)
    {

        pCammerWidget->m_qDisplayFrameQueue.push_back(mat_image.clone());
    }
    else
    {
            VectorOutputResult result;
            result.m_detectin_result = true;
            result.m_camera_index = pCammerWidget->m_camera_index;
            result.m_detection_index = pCammerWidget->m_image_index;
            result.m_camera_name = pCammerWidget->m_camera_name;
            result.m_save_right = pCammerWidget->m_function->m_save_right;
            result.m_save_wrong =pCammerWidget-> m_function->m_save_wrong;
            result.m_image = mat_image.clone();
            result.m_error_number = pCammerWidget->m_error_number;
            result.m_all_number = pCammerWidget->m_all_number;
            result.m_proess_state = pCammerWidget->m_proess_state;
            result.m_save_realtime=pCammerWidget->m_save_realtime;
            pCammerWidget->m_save_realtime--;
            emit pCammerWidget->sendDetectionResult(result);
    }
    if (pCammerWidget->m_show_camera_dialog == true)
    {

        emit pCammerWidget->sendShowImage(mat_image, pCammerWidget->m_camera_index, pCammerWidget->m_image_index);

    }


}

QtThreadProcess::QtThreadProcess(QObject* parent) 
{
    m_proess_state = false;
    m_show_camera_dialog = false;

    qRegisterMetaType<Mat>("Mat");
    //对于自定义的结构体参数，信号槽无法识别参数，导致信号槽连接不起作用。
    //所以需要注册结构体参数。在结构体中声明结束的地方加上结构体注册。
    qRegisterMetaType<VectorOutputResult>("VectorOutputResult");
}

void QtThreadProcess::run()
{
    while (1)
        {

            Mat image;
            if (false == m_qDisplayFrameQueue.get(image))
            {
                continue;
            }
            if (m_proess_state == true)
            {
                m_detection_index++;
                /*if(m_detection_index>m_function->m_times)
                {
                    continue;
                }*/
                //logger->info("camera:" + to_string(m_camera_index) + "index:" + to_string(m_detection_index));
                int64 t1 = cv::getTickCount();

                HObject hobject = matToHImage(image);
                VectorOutputResult result;

               
                result.m_detectin_result =m_function->detection(hobject, result.m_messgae);


                result.m_image = image.clone();
                result.m_camera_index = m_camera_index;

                result.m_detection_index = m_detection_index;
                result.m_camera_name = m_camera_name;
                result.m_save_right = m_function->m_save_right;
                result.m_save_wrong = m_function->m_save_wrong;
                result.m_proess_state = m_proess_state;
                
                m_vector_result.push_back(m_function->m_vector_result);

                if(m_vector_result.size()>=m_function->m_times)
                {
                    vector<int> vector_int_result;
                    vector_int_result.assign(m_function->m_number_rows * m_function->m_ball_number, 1);
                    for(int i=0;i<m_function->m_times ;i++)
                    {
                        for(int j=0;j<m_vector_result[i].size();j++)
                        {
                            if(m_vector_result[i][j]<=0)
                            {
                                vector_int_result[j]=m_vector_result[i][j];
                            }

                        }
                    }

                    for(int i=0;i<vector_int_result.size();i++)
                    {
                        if(vector_int_result[i]<0)
                        {
                            m_error_number++;
                        }
                        if (vector_int_result[i] != 0)
						{
							m_all_number++;
						}

                    }
					result.m_vector_orign_result.clear();
					result.m_vector_orign_result.assign(vector_int_result.begin(), vector_int_result.end());
					//one row
                    vector<unsigned int> vector_result;
                    if(m_function->m_number_rows==1)
                    {
                        vector_result.resize(m_function->m_number_rows * m_function->m_ball_number);
                        for(int i=0;i<vector_int_result.size();i++)
                        {
                            if(vector_int_result[i]>0)
                            {
                                vector_result[i]=1;
                            }
                            else
                            {
                                vector_result[i]=0;
                            }
                        }
                    }
					//two row
                    if(m_function->m_number_rows==2)
                    {
                        int row_number  = m_function->m_ball_number;
						
						
                        vector_result.resize(row_number);
                        for(int i=0;i<row_number;i++)
                        {
                            if(vector_int_result[i]>0 &&  vector_int_result[i+row_number]>0)
                            {
                                vector_result[i]=1;
                            }
                            if(vector_int_result[i] &&  vector_int_result[i+row_number]<=0)
                            {
                                vector_result[i]=2;
                            }
                            if(vector_int_result[i]<=0 &&  vector_int_result[i+row_number])
                            {
                                vector_result[i]=3;
                            }
                            if(vector_int_result[i]<=0 &&  vector_int_result[i+row_number]<=0)
                            {
                                vector_result[i]=0;
                            }
                        }
                    }

                    result.m_vector_result.push_back(vector_result);
                    m_vector_result.clear();
                }


                int64 t2 = cv::getTickCount();

                m_time_use = (t2 - t1) * 1000 / getTickFrequency();
                result.m_time_use = m_time_use;
				
                
				result.m_error_number = m_error_number;
				result.m_all_number = m_all_number;
                
//                if (m_detection_index == m_function->m_times)
//                {
//                    m_detection_index = 0;
//                }
				emit sendDetectionResult(result);
            }

    }
}
